#include "UserInfoDialog.h"
#include <QGridLayout>
#include "Model/DataCenter.h"
#include "MainWidget.h"
#include <QMessageBox>
#include "Toast.h"
using namespace model;

/////////////////////////////////////////////
/// 点击其它用户头像时显示的画面
/////////////////////////////////////////////
UserInfoDialog::UserInfoDialog(QWidget* parent, const model::UserInfo& user_info)
    : QDialog(parent), user_info(user_info)
{
    init_ui();
    connect_signals();
}

/// @brief 初始化UI操作
void UserInfoDialog::init_ui()
{
    this->setWindowTitle("用户信息");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setFixedSize(420, 320);
    this->setAttribute(Qt::WA_DeleteOnClose);
    QGridLayout* g_layout = new QGridLayout();
    g_layout->setContentsMargins(0, 0, 0, 15);
    g_layout->setSpacing(0);
    this->setLayout(g_layout);
    /* 1. 初始化头像 */
    avatar = new CircleButton();
    avatar = new CircleButton();
    avatar->setFixedSize(70, 70);
    avatar->setIconSize(QSize(70, 70));
    avatar->setIcon(user_info.avatar);
    /* 2. 初始化各种Labels */
    initLabels(oo, "OO", user_info.user_id);
    initLabels(nick_name, "昵称", user_info.nick_name);
    initLabels(desc, "个签", user_info.description);
    initLabels(phone_num, "电话", user_info.phone_num);
    /* 3. 初始化各种按钮 */
    QWidget*     btn_container = new QWidget();
    QHBoxLayout* h_layout      = new QHBoxLayout();
    btn_container->setLayout(h_layout);
    h_layout->setContentsMargins(0, 0, 0, 0);
    initButtons(add, "添加好友");
    initButtons(send, "发送消息");
    initButtons(del, "删除好友");
    h_layout->addWidget(add);
    h_layout->addWidget(send);
    h_layout->addWidget(del);
    /* 4. 设置layout */
    g_layout->addWidget(avatar, 0, 0, 1, 3, Qt::AlignHCenter);
    g_layout->addWidget(oo.show_label, 1, 0, 1, 1, Qt::AlignHCenter);
    g_layout->addWidget(oo.edit_label, 1, 1, 1, 2);
    g_layout->addWidget(nick_name.show_label, 2, 0, 1, 1, Qt::AlignHCenter);
    g_layout->addWidget(nick_name.edit_label, 2, 1, 1, 2);
    g_layout->addWidget(desc.show_label, 3, 0, 1, 1, Qt::AlignHCenter);
    g_layout->addWidget(desc.edit_label, 3, 1, 1, 2);
    g_layout->addWidget(phone_num.show_label, 4, 0, 1, 1, Qt::AlignHCenter);
    g_layout->addWidget(phone_num.edit_label, 4, 1, 1, 2);
    g_layout->addWidget(btn_container, 5, 0, 1, 3);
    /* 获得 data_center */
    data_center = DataCenter::get_instance();
    /* 设置按钮的状态 */
    if (data_center->is_friend(user_info.user_id)) {
        /* 是好友 */
        add->setDisabled(true); 
    } else {
        /* 不是好友 */
        send->setDisabled(true);
        del->setDisabled(true);
    }
}

/// @brief 连接信号槽
void UserInfoDialog::connect_signals()
{
    /* 是好友时, 点击发送按钮 */
    connect(send, &QPushButton::clicked, this, &UserInfoDialog::handle_send_clicked);
    /* 是好友时, 点击删除按钮 */
    connect(del, &QPushButton::clicked, this, &UserInfoDialog::handle_del_clicked);
    /* 不是好友时, 点击添加按钮 */
    connect(add, &QPushButton::clicked, this, &UserInfoDialog::handle_add_clicked);
}

/// @brief: 初始化Label的重复操作
void UserInfoDialog::initLabels(Labels& labels, const QString& text1, const QString& text2)
{
    labels.show_label = new QLabel();
    labels.show_label->setStyleSheet(
        "QLabel{"
        "font-weight: 600;"
        "font-size: 14px;"
        "}"); // 让其加粗
    labels.show_label->setText(text1);
    labels.edit_label = new QLabel();
    labels.edit_label->setStyleSheet(
        "QLabel {"
        "font-size: 14px;"
        "}");
    labels.edit_label->setText(text2);
}

/// @brief 初始化按钮的重复操作
void UserInfoDialog::initButtons(QPushButton*& btn, const QString& text)
{
    btn = new QPushButton();
    btn->setText(text);
    btn->setFixedSize(QSize(80, 30));
    btn->setStyleSheet(
        "QPushButton{"
        "background-color: #0099FF;"
        "border: none;"
        "border-radius: 5px;"
        "}"
        "QPushButton:hover {"
        "background-color: #0093F5;"
        "}"
        "QPushButton:pressed {"
        "background-color: #0086E0;"
        "}");
}

/// @brief 点击发送消息按钮
void UserInfoDialog::handle_send_clicked()
{
    MainWidget* main_widget = MainWidget::get_instance();
    main_widget->handle_friend_item_active(user_info.user_id);
    this->close();
}

/// @brief 点击删除好友按钮
void UserInfoDialog::handle_del_clicked()
{
    auto res = QMessageBox::warning(this, "删除好友", "确定删除好友?", QMessageBox::Ok | QMessageBox::Cancel);
    if (res != QMessageBox::Ok) {
        Toast::showMessage("取消删除好友");
        return;
    }
    data_center->del_friend_async(user_info.user_id);
    this->close();
}

/// @brief 点击添加好友按钮
void UserInfoDialog::handle_add_clicked()
{
    data_center->add_friend_async(user_info.user_id);
    this->close();
}
